<%@LANGUAGE="VBSCRIPT" CODEPAGE="65001"%>
<%
Option Explicit
%>
<!--#include file="Common/Function.asp"-->
<!--#include file="Config.asp"-->
<!--#include file="Library/db.asp"-->
<!--#include file="Library/request.asp"-->
<!--#include file="Library/response.asp"-->
<!--#include file="Library/session.asp"-->
<!--#include file="Library/cookie.asp"-->
<!--#include file="Library/cache.asp"-->
<!--#include file="Library/template.asp"-->
<!--#include file="Library/language.asp"-->
<!--#include file="Library/debug.asp"-->
<!--#include file="Library/trace.asp"-->
<!--#include file="Library/log.asp"-->
<!--#include file="Library/fso.asp"-->
<!--#include file="Library/upload.asp"-->
<!--#include file="Library/image.asp"-->
<!--#include file="Library/excel.asp"-->
<!--#include file="Library/markdown.asp"-->
<!--#include file="Library/page.asp"-->
<!--#include file="Library/crypt.asp"-->
<!--#include file="Library/validate.asp"-->
<!--#include file="Library/json.asp"-->
<%
Const FastAsp_Version = "1.0.0"
Const FastAsp_Path = "FastAsp"
Class FastAsp
    Private m_DB
    Private m_Request
    Private m_Response
    Private m_Session
    Private m_Cookie
    Private m_Cache
    Private m_Template
    Private m_Lang
    Private m_Debug
    Private m_Trace
    Private m_Log
    Private m_FSO
    Private m_Upload
    Private m_Image
    Private m_Excel
    Private m_Markdown
    Private m_Page      '分页类
    Private m_Crypt     '加密类
    Private m_Validate  '验证类
    Private m_JSON      'JSON类
    
    Private Sub Class_Initialize()
        Set m_DB = New FastAsp_DB
        Set m_Request = New FastAsp_Request
        Set m_Response = New FastAsp_Response
        Set m_Session = New FastAsp_Session
        Set m_Cookie = New FastAsp_Cookie
        Set m_Cache = New FastAsp_Cache
        Set m_Template = New FastAsp_Template
        Set m_Lang = New FastAsp_Language
        Set m_Debug = New FastAsp_Debug
        Set m_Trace = New FastAsp_Trace
        Set m_Log = New FastAsp_Log
        Set m_FSO = New FastAsp_FSO
        Set m_Upload = New FastAsp_Upload
        Set m_Image = New FastAsp_Image
        Set m_Excel = New FastAsp_Excel
        Set m_Markdown = New FastAsp_Markdown
        Set m_Page = New FastAsp_Page
        Set m_Crypt = New FastAsp_Crypt
        Set m_Validate = New FastAsp_Validate
        Set m_JSON = New FastAsp_JSON
    End Sub
    
    '获取数据库对象
    Public Property Get DB
        Set DB = m_DB
    End Property
    
    '获取请求对象
    Public Property Get Request
        Set Request = m_Request
    End Property
    
    '获取响应对象
    Public Property Get Response
        Set Response = m_Response
    End Property
    
    '获取会话对象
    Public Property Get Session
        Set Session = m_Session
    End Property
    
    '获取Cookie对象
    Public Property Get Cookie
        Set Cookie = m_Cookie
    End Property
    
    '获取缓存对象
    Public Property Get Cache
        Set Cache = m_Cache
    End Property
    
    '获取模板引擎对象
    Public Property Get Template
        Set Template = m_Template
    End Property
    
    '获取语言包对象
    Public Property Get Lang
        Set Lang = m_Lang
    End Property
    
    '获取调试对象
    Public Property Get Debug
        Set Debug = m_Debug
    End Property
    
    '获取跟踪对象
    Public Property Get Trace
        Set Trace = m_Trace
    End Property
    
    '获取日志对象
    Public Property Get Log
        Set Log = m_Log
    End Property
    
    '获取文件系统对象
    Public Property Get FSO
        Set FSO = m_FSO
    End Property
    
    '获取上传对象
    Public Property Get Upload
        Set Upload = m_Upload
    End Property
    
    '获取图片处理对象
    Public Property Get Image
        Set Image = m_Image
    End Property
    
    '获取Excel对象
    Public Property Get Excel
        Set Excel = m_Excel
    End Property
    
    '获取Markdown对象
    Public Property Get Markdown
        Set Markdown = m_Markdown
    End Property
    
    '获取分页对象
    Public Property Get Page
        Set Page = m_Page
    End Property
    
    '获取加密对象
    Public Property Get Crypt
        Set Crypt = m_Crypt
    End Property
    
    '获取验证对象
    Public Property Get Validate
        Set Validate = m_Validate
    End Property
    
    '获取JSON对象
    Public Property Get JSON
        Set JSON = m_JSON
    End Property
    
    '常用方法
    Public Function [get](byval key)
        [get] = m_Request.[get](key)
    End Function
    
    Public Function post(byval key)
        post = m_Request.post(key)
    End Function
    
    Public Sub redirect(byval url)
        m_Response.redirect url
    End Sub
    
    '模板引擎快速方法
    Public Function assign(byval key, byval value)
        m_Template.Assign key, value
        Set assign = Me
    End Function
    
    Public Function display(byval file)
        m_Template.Display file
        Set display = Me
    End Function
    
    Public Function fetch(byval file)
        fetch = m_Template.Fetch(file)
    End Function
    
    '快速分页方法
    Public Function Pager(byval total, byval style)
        m_Page.TotalRows = total
        If style <> "" Then m_Page.Style = style
        Pager = m_Page.Show()
    End Function
    
    '快速加密方法
    Public Function MD5(byval str)
        MD5 = m_Crypt.MD5(str)
    End Function
    
    Public Function SHA1(byval str)
        SHA1 = m_Crypt.SHA1(str)
    End Function
    
    Public Function AESEncrypt(byval str)
        AESEncrypt = m_Crypt.AESEncrypt(str)
    End Function
    
    Public Function AESDecrypt(byval str)
        AESDecrypt = m_Crypt.AESDecrypt(str)
    End Function
    
    '快速验证方法
    Public Function Verify(byval data, byval rules)
        Dim dict : Set dict = Server.CreateObject("Scripting.Dictionary")
        dict.Add "data", data
        dict.Add "rules", rules
        Verify = m_Validate.Check(dict)
    End Function
    
    '快速JSON方法
    Public Function ToJSON(byval data)
        ToJSON = m_JSON.Encode(data)
    End Function
    
    Public Function FromJSON(byval jsonStr)
        Set FromJSON = m_JSON.Decode(jsonStr)
    End Function
    
    '快速响应方法
    Public Function ContentType(byval type_)
        m_Response.ContentType type_
        Set ContentType = Me
    End Function
    
    Public Function SetCharset(byval charset)
        m_Response.SetCharset charset
        Set SetCharset = Me
    End Function
    
    Public Function Status(byval code)
        m_Response.Status code
        Set Status = Me
    End Function
    
    Public Function Header(byval name, byval value)
        m_Response.Header name, value
        Set Header = Me
    End Function
    
    '快速成功响应
    Public Sub Success(byval msg, url, wait)
        if IsEmpty(url) then url = ""
        if IsEmpty(wait) then wait = 3
        m_Response.Success msg, url, wait
    End Sub
    
    '快速错误响应
    Public Sub Error_(byval msg, url, wait)
        if IsEmpty(url) then url = ""
        if IsEmpty(wait) then wait = 3
        m_Response.Error_ msg, url, wait
    End Sub
    
    '快速JSON响应
    Public Sub WriteJson(byval data)
        On Error Resume Next
        Dim dict : Set dict = Server.CreateObject("Scripting.Dictionary")
        
        If TypeName(data) = "String" Then
            m_Response.WriteJson data
        ElseIf TypeName(data) = "Dictionary" Then
            '如果是字典对象，确保有code字段
            If Not data.Exists("code") Then data.Add "code", 1
            m_Response.WriteJson data
        ElseIf TypeName(data) = "Array" Then
            '支持数组格式：array("key1","value1","key2","value2",...)
            dict.Add "code", 1
            Dim i
            For i = 0 To UBound(data) Step 2
                If i + 1 <= UBound(data) Then
                    dict.Add data(i), data(i + 1)
                End If
            Next
            m_Response.WriteJson dict
        Else
            '其他类型包装成标准格式
            dict.Add "code", 1
            dict.Add "data", data
            m_Response.WriteJson dict
        End If
        On Error Goto 0
    End Sub
    
    '输出原始JSON数据(不包装code和data)
    Public Sub WriteRawJson(byval data)
        On Error Resume Next
        If TypeName(data) = "String" Then
            '如果是字符串且已经是JSON格式则直接输出
            If (Left(data, 1) = "{" And Right(data, 1) = "}") Or _
               (Left(data, 1) = "[" And Right(data, 1) = "]") Then
                m_Response.WriteJson data
            Else
                '非JSON格式字符串转为JSON字符串
                m_Response.WriteJson m_JSON.Encode(data)
            End If
        ElseIf TypeName(data) = "Array" Then
            '数组格式：array("key1","value1","key2","value2",...)转换为对象
            Dim dict : Set dict = Server.CreateObject("Scripting.Dictionary")
            Dim i
            For i = 0 To UBound(data) Step 2
                If i + 1 <= UBound(data) Then
                    dict.Add data(i), data(i + 1)
                End If
            Next
            m_Response.WriteJson dict
        Else
            '其他类型直接JSON编码
            m_Response.WriteJson data
        End If
        On Error Goto 0
    End Sub
    
    '快速404响应
    Public Sub Show404()
        m_Response.Show404()
    End Sub
    
    '显示维护页面
    Public Sub ShowMaintain(byval data)
        m_Response.ShowMaintain data
    End Sub
    
    Private Sub Class_Terminate()
        Set m_DB = Nothing
        Set m_Request = Nothing
        Set m_Response = Nothing
        Set m_Session = Nothing
        Set m_Cookie = Nothing
        Set m_Cache = Nothing
        Set m_Template = Nothing
        Set m_Lang = Nothing
        Set m_Debug = Nothing
        Set m_Trace = Nothing
        Set m_Log = Nothing
        Set m_FSO = Nothing
        Set m_Upload = Nothing
        Set m_Image = Nothing
        Set m_Excel = Nothing
        Set m_Markdown = Nothing
        Set m_Page = Nothing
        Set m_Crypt = Nothing
        Set m_Validate = Nothing
        Set m_JSON = Nothing
    End Sub
End Class

'创建全局对象
Dim Fast
Set Fast = New FastAsp
%>
